/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.vod.gauss.encoder;

import fr.vod.gauss.operations.Operation;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 *
 * @author dRazanakoto
 */
public class EncoderImp implements Encoder {

    /**
     *
     */
    private Operation op;
    private final Random rand;

    /**
     *
     * @param op
     */
    public EncoderImp(Operation op) {
        this.op = op;
        rand = new Random();
    }

    @Override
    public int calculCheckSum(int[] data, int[] symbole) {
        int res = 0;
        if (data.length != symbole.length) {
            return -1;
        }

        for (int i = 0; i < data.length; i++) {
            res = op.addition(op.multiplication(data[i], symbole[i]), res);
        }
        return res;
    }

    @Override
    public int[] generateSymboles(int n) {
        int tab[] = new int[n];
        for (int i = 0; i < n; i++) {
            tab[i] = rand.nextInt(255) + 1;
        }
        return tab;
    }

    @Override
    public int[] generateDataIndice(int max, int n) {
        List<Integer> vector = createVectorNumber(max);
        int[] tab = new int[n];
        int tmp;
        for (int i = 0; i < n; i++) {
            tmp = rand.nextInt(vector.size());
            tab[i] = vector.get(tmp);
            vector.remove(new Integer(tab[i]));
        }
        return tab;
    }

    /**
     *
     * @return
     */
    public Operation getOp() {
        return op;
    }

    /**
     *
     * @param op
     */
    public void setOp(Operation op) {
        this.op = op;
    }

    /**
     *
     * @param nb
     * @return
     */
    protected List<Integer> createVectorNumber(int nb) {
        List<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < nb; i++) {
            list.add(new Integer(i));
        }
        return list;
    }
}
